

function _getAmzElementsByXPath(expression,parent) {
    var results = [];
    var query = parent.evaluate(expression, parent || window.content.document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0, length = query.snapshotLength; i < length; i++)
        results.push(query.snapshotItem(i));
    return results;
};
function getAmzElementsByClassName(className,parent) {
    var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
    return _getAmzElementsByXPath(q,parent);
}


function AmazonProductRequest(product){
	
	this.product = product;
	
	//result url from tinurl
	this.amazonPriceResult = null;
	
	//amazon httprequest
	this.request = null;
	
	//execute the tiny url request
	this.execute = function(callback){
		
		// No need to shrink
		if(this.amazonPriceResult != null){
			return;
		}
		
		// check if there is an outstanding request
		if(this.request != null){
			this.request.abort();
		}
		
				//url substitutions
		var keyword = this.product;
   	keyword = keyword.replace(/\+/g, "%2B").replace(/&/g, '%26').replace(/\?/g, '%3F').replace(/=/g, '%3D').replace(/\s/,"%20"); 
		
		var aurl = "http://www.amazon.com/exec/obidos/external-search?mode=blended&keyword=" + keyword;
		// Setup Request	
		this.request	= new XMLHttpRequest();
		//this.request.open("GET", "http://www.amazon.com/exec/obidos/external-search", true); //(default)true:async - false: sync
		this.request.open("GET", aurl, true); //(default)true:async - false: sync
		//this.request.open("GET", "http://www.amazon.com/exec/obidos/external-search?mode=blended&keyword=bioshock", true); //(default)true:async - false: sync
		this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.request.setRequestHeader("User-Agent", navigator.userAgent);
		//this.request.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

		
      	
		
		//store reference to stuff
		var handle = this;

		//alert('apr 46');
		// Handle request
		this.request.onload = {
			handleEvent : function(e){
				
				// If already aborted
				if(handle.request == null){
					return;
				}
				
				//get html from response and look for tunyurl
				var html = handle.request.responseText;
				handle.amazonPriceResult = null;
				alert('amazon response ' + html);
				//var _amzResultRegExStr = 'td class=\"(priceBlockWithTopPadding)\"';
				//var _amzResultRegExStr = 'bioshock';
				//1 get price but not product url
				//1 var _amzResultRegExStr = "<td class=\"dataColumn\">\\s*<a\\s+href=\"([^\"'\\s>]*)\">.*|\\s*class=\"sr_price\">(\\$59\\.99)<\\/span>";  //^\d+(?:\.\d{0,2})?$
				//2 product and $59.99
				//var _amzResultRegExStr = "<td class=\"dataColumn\">\\s*.*\\s*.*\\s*.*\\s*.*<a\\s+href=\"([^\"'\\s>]*)\">.*|\\s*class=\"sr_price\">(\\$59\\.99)<\\/span>";
				//3 var _amzResultRegExStr = "<td class=\"dataColumn\">\\s*.*\\s*.*\\s*.*\\s*.*<a\\s+href=\"([^\"'\\s>]*)\">\\s*|.*class=\"(sr_price|saleprice)\">(\\$\\d+(?:\\.\\d{0,2})?)<\\/span>";
				
				var _amzResultRegExStr = "<td class=\"dataColumn\">\\s*.*\\s*.*\\s*.*\\s*.*<a\\s+href=\"([^\"'\\s>]*)\">.*|\\s*class=\"(sr_price|saleprice)\">(\\$\\d+(?:\\.\\d{0,2})?)<\\/span>";
				//var _amzResultRegExStr = "<td class=\"dataColumn\">\\s*.*\\s*.*\\s*.*\\s*.*<a\\s+href=\"([^\"'\\s>]*)\">\\s*(<span>.*|\\s*<\\/span>)|(<tr>.*|\\s*<\\/tr>|<td>.*|\\s*<\\/td>)*class=\"(sr_price|saleprice)\">(\\$\\d+(?:\\.\\d{0,2})?)<\\/span>";
				var regexp	= new RegExp(_amzResultRegExStr, "ig");
				//var azresult	= regexp.exec(html);
				var azresult = null;
				azresult = regexp.exec(html);
				alert('none loop execute azresult 1: ' + azresult[1] + ' 2: ' + azresult[2] + ' 3: ' + azresult[3]);
				while ((azresult = regexp.exec(html)) != null) {
					alert('while azresult ' + azresult);
				}
				if(azresult != null && azresult.length > 0){
					alert('result at 0 ' + azresult[1])
					handle.amazonPriceResult = azresult[1];  				
				}
				/*
				var parser=new DOMParser();
				var resultDoc=parser.parseFromString(html,"text/html");   //not yet supported
				var srprice = getAmzElementsByClassName('sr_price',resultDoc);
				alert('srprice array ' + srprice);
				*/
				
				// callback to onerror or ondone
				if(handle.amazonPriceResult == null || handle.amazonPriceResult==""){
					callback.onerror(handle);
				}
				else{
					//should just be sending back the tunyurl as a string
					callback.ondone(handle);
				}
			}
		};
		
		// check if the xmlhttprequest had an error--if so, callback to error
		this.request.onerror = {
			handleEvent : function(e){
				callback.onerror(handle);
			}
		}
       	      	
    //send request
    //this.request.send("url="+ url); mode=blended
   // this.request.send("mode=blended&keyword="+ keyword);
    this.request.send(null);
	}
	

//cancel the request
	this.cancel = function(){
		try{
			this.request.abort();
		}catch(e){  }
	}	
}